package initialize

import (
	"fmt"
	"go_final/mxshop_api/userop_web/global"
	addresspb "go_final/mxshop_api/userop_web/proto/protofile/address"
	messagepb "go_final/mxshop_api/userop_web/proto/protofile/message"
	user_favpb "go_final/mxshop_api/userop_web/proto/protofile/user_fav"

	_ "github.com/mbobakov/grpc-consul-resolver" // It's important
	"go.uber.org/zap"
	"google.golang.org/grpc"
)

// InitSrvConnWithLoadBalance 负载均衡拉取服务
func InitSrvConnWithLoadBalance() {
	conn, err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s",
			global.ServerConfig.ConsulInfo.Host,
			global.ServerConfig.ConsulInfo.Port,
			global.ServerConfig.UseropSrcInfo.Name,
		),
		grpc.WithInsecure(),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		zap.S().Info("[InitSrvConnWithLoadBalance] 连接 【商品服务】失败")
	}

	global.AddressClient = addresspb.NewAddressClient(conn)
	global.UserFavClient = user_favpb.NewUserFavClient(conn)
	global.MessageClient = messagepb.NewMessageClient(conn)
}
